package com.zxy.broker.config;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zxy.broker.dao.HousesDao;
import com.zxy.broker.entity.HousesEntity;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class Esrun implements ApplicationRunner {
    @SuppressWarnings("all")
    @Autowired
    RestHighLevelClient client;
    @SuppressWarnings("all")
    @Autowired
    HousesDao housesDao;
    @Override
    public void run(ApplicationArguments args) throws Exception {
        QueryWrapper<HousesEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("id",0);
        List<HousesEntity> housesEntities = housesDao.selectList(null);
        BulkRequest bulkRequest = new BulkRequest("houses");
        for (HousesEntity housesEntity : housesEntities) {
            IndexRequest request = new IndexRequest();
            request.id(housesEntity.getId()+"");
            String jsonString = JSON.toJSONString(housesEntity);
            request.source(jsonString, XContentType.JSON);
            bulkRequest.add(request);
        }
        client.bulk(bulkRequest,GuliESConfig.COMMON_OPTIONS);
    }
}
